package ifi.p17.jarmanagement.view;

import java.util.ArrayList;
import java.util.Vector;

import ifi.p17.jarmanagement.R;
import ifi.p17.jarmanagement.control.FileProcess;
import ifi.p17.jarmanagement.control.TimeDate;
import ifi.p17.jarmanagement.model.LogErorr;
import ifi.p17.jarmanagement.model.Purchase;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class Statistics extends Activity{
	
	EditText et_year, et_month, et_day;
	TextView et_today;
	Button b_ok;
	ListView lview;
	ArrayList<Purchase> purchases;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.statistics);
		
		loadVariables();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
//		
//		menu.add(0, Menu.FIRST, 0 , "Delete", );
		return super.onCreateOptionsMenu(menu);
	}
	
	private void loadVariables() {
		et_year = (EditText) findViewById(R.id.search_year);
		et_month = (EditText) findViewById(R.id.search_month);
		et_day = (EditText) findViewById(R.id.search_day);
		et_today = (TextView) findViewById(R.id.search_today);
		b_ok = (Button) findViewById(R.id.search_ok);
		lview = (ListView) findViewById(R.id.list_purchase);
		
		showListToday();
		
		b_ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				actionSearch(v);
			}
		});
	}

	private void showListToday() {
		TimeDate td = new TimeDate();
		et_today.setText(td.getYearMonthDay());
		loadPurchasesOnDate(td.getYearMonthDay());

		updateListView(reverse(purchases));
	}

	protected void actionSearch(View v) {
		// verify input
		String year = et_year.getText().toString();
		String month = et_month.getText().toString();
		String day = et_day.getText().toString();

		if (year.length()>0 && month.length()>0 && day.length()>0){
			// search on day
			String date = year + "/" + month + "/" + day;
			String file = FileProcess.HOME_DIR + FileProcess.APP_DIR + date;
			searchOnDay(v, date, file);
		} else if (year.length()>0 && month.length()>0 && day.length()==0) {
			// search on month
			String search_month = year + "/" + month;
			searchOnMonth(v, search_month);
		} else if (year.length()>0 && month.length()==0 && day.length()==0){
			String search_year = year;
			searchOnYear(v, search_year);
		}
		
		
	}
	
	private void searchOnYear(View v, String search_year) {
		String file = FileProcess.HOME_DIR + FileProcess.APP_DIR + search_year;
		if (!FileProcess.fileExist(file)) {
			AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
			
			builder.setTitle("Waring")
					.setMessage("Input format error or no purchase in this year");
			
			builder
				.setCancelable(false)
				.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});
			
			builder.create().show();
		} else {
			et_today.setText(search_year);
			ArrayList<Purchase> monthInYear = new ArrayList<Purchase>();
			FileProcess.getAllMonthInYear(search_year, monthInYear);
			updateListView(monthInYear);
		}
	}

	private void searchOnMonth(View v, String search_month) {
		String file = FileProcess.HOME_DIR + FileProcess.APP_DIR + search_month;
		if (!FileProcess.fileExist(file)) {
			AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
			
			builder.setTitle("Waring")
					.setMessage("Imput format error or no purchase in this month");
			
			builder
				.setCancelable(false)
				.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});
			
			builder.create().show();
		} else {
			et_today.setText(search_month);
			ArrayList<Purchase> dayinmonth = new ArrayList<Purchase>();
			FileProcess.getAllDayInMonth(search_month, dayinmonth);
			updateListView(dayinmonth);
		}
	}

	private void searchOnDay(View v, String date, String file){
		if (!FileProcess.fileExist(file)) {
			// alert
			AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
			
			builder.setTitle("Waring")
					.setMessage("Input format error or no purchase in this date");
			
			builder
				.setCancelable(false)
				.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});
			
			builder.create().show();
		} else {
			// show list
			et_today.setText(date);
			loadPurchasesOnDate(date);
			updateListView(reverse(purchases));
		}
	}
	
	
	private void updateListView(ArrayList<Purchase> array) {
		
//		ArrayList<Purchase> array = new ArrayList<Purchase>();
//		for (int i = purchases.size()-1; i>=0; i--)
//			array.add(purchases.get(i));
		CustomListAdapter arrayAdapter = new CustomListAdapter(this, R.layout.list_item, array);
		
		lview.setAdapter(arrayAdapter);
		
	}

	private void loadPurchasesOnDate(String date) {
		purchases = new ArrayList<Purchase>();
		FileProcess.loadPurchase(purchases, date);
	}
	
	private ArrayList<Purchase> reverse(ArrayList<Purchase> arr){
		ArrayList<Purchase> re = new ArrayList<Purchase>();
		for (int i = arr.size()-1; i>=0; i--)
			re.add(arr.get(i));
		return re;
	}
	
}
